<?php

/**
 * This is the model class for table "cabs_image".
 *
 * The followings are the available columns in table 'cabs_image':
 * @property integer $id
 * @property string $name
 * @property string $slug
 * @property string $mediaFormat
 * @property string $url
 * @property integer $thumbnail
 * @property string $description
 * @property string $dateTaken
 * @property integer $galleryId
 * @property string $create_time
 * @property integer $create_user_id
 * @property string $update_time
 * @property integer $update_user_id
 *
 * The followings are the available model relations:
 * @property Extra[] $extras
 * @property Gallery $gallery
 */
class Image extends CabsActiveRecord
{
const MEDIA_IMAGE='Image';
const MEDIA_VIDEO='Video';
public $theImage;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cabs_image';
	}
	public $gallery_grid;

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, galleryId', 'required'),
			array('galleryId', 'numerical', 'integerOnly'=>true),
			array('thumbnail', 'boolean'),
			array('mediaFormat', 'length', 'max'=>5),
			array('url', 'safe'),
			array('name, slug', 'length', 'max'=>255),
			array('url, description, dateTaken', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, slug, mediaFormat, url, thumbnail, description, dateTaken, galleryId, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),
			array('theImage', 'file', 'types'=>'jpg, gif, png','on'=>array('create')),
			array('theImage', 'file', 'allowEmpty'=>true, 'types'=>'jpg, gif, png', 'on'=>'update'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'extras' => array(self::HAS_MANY, 'Extra', 'imageId'),
			'gallery' => array(self::BELONGS_TO, 'Gallery', 'galleryId'),
);
}
	public function scopes()
	{
	return array(
	'isThumbnail'=>array(
	'condition'=>"thumbnail=1",
	),
	);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'slug' => 'Slug',
			'description' => 'Description',
			'mediaFormat' => 'Media Format',
			'url' => 'Video Url',
			'thumbnail' => 'Thumbnail',
			'dateTaken' => 'Date Taken',
			'galleryId' => 'Gallery',
			'gallery_grid' => 'Gallery',
			'create_time' => 'Created On',
			'create_user_id' => 'Created By',
			'update_time' => 'Last Updated On',
			'update_user_id' => 'Last Updated By',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('slug',$this->slug,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('mediaFormat',$this->mediaFormat,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('thumbnail',$this->thumbnail);
		$criteria->compare('dateTaken',$this->dateTaken,true);
		$criteria->compare('galleryId',$this->galleryId);
		$criteria->compare('gallery.name',$this->gallery_grid, true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('create_user_id',$this->create_user_id);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user_id',$this->update_user_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
				'attributes'=>array(
					'gallery_grid'=>array(
						'asc'=>'gallery.name',
						'desc'=>'gallery.name DESC',
					),
					'*',
				),
			),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Image the static model class
	 */
	public function getGalleryOptions()
	{
		$galleryModel = Gallery::model()->findAll(
			array('order'=>'name'));
		$galleryArray = CHtml::listData($galleryModel, 'id','name');
		return $galleryArray;
	}
	public function getMediaOptions()
	{
		return array(
		self::MEDIA_IMAGE=>'Image',
		self::MEDIA_VIDEO=>'Video',
		);
	}

	public function getMediaText()
	{
		$mediaOptions=$this->$mediaOptions;
		return isset($mediaOptions[$this->mediaFormat]) ? $mediaOptions[$this->mediaFormat] : "unknown value ({$this->mediaFormat})";
	}

	public function getMediaDisplay()
	{
		return $this->name;
	}

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	}

